package com.poc.oauth.resources.conf;

import com.poc.oauth.resources.web.controller.HelloWorldController;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class ServletInitializer extends AbstractDispatcherServletInitializer {
    private static final Logger log = Logger.getLogger(ServletInitializer.class);

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        log.debug("in createServletApplicationContext()");
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(ClassUtils.getPackageName(getClass()));
        return context;
    }

    @Override
    protected String[] getServletMappings() {
        log.debug("in getServletMappings");
        return new String[] { "/" };
    }

    @Override
    protected String getServletName() {
        return "resources-mvc-servlet";
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
//        DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
//        filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
//        servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        log.debug("in createRootApplicationContext()");
        return null;
    }
}
